//
//  ZYCSearchBarView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/22.
//

#import "ZYCSearchBarView.h"
@interface ZYCSearchBarView ()<UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, assign) CGFloat height;
@end
@implementation ZYCSearchBarView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        // 监听键盘的弹出及收起
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        //监听当键将要退出时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    return self;
}


#pragma mark - Notification Method

// 键盘弹出
- (void)keyboardWillShow:(NSNotification *)notification{
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.height = keyboardFrame.size.height;
    [UIView animateWithDuration:duration animations:^{
        
    }];
}
// --键盘收回
- (void)keyboardWillHide:(NSNotification *)notification{
    self.height = 0;
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
          
    }];
}

- (void)initUI{
    [self addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35.0f);
        make.left.equalTo(self).offset(15.0f);
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-15.0f);
    }];
}

-(UISearchBar *) searchBar{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        _searchBar.placeholder = @"输入姓名搜索";
        _searchBar.layer.cornerRadius = 17.5f;
        _searchBar.layer.masksToBounds = YES;
        _searchBar.layer.borderColor = HEX_RGB(@"#AAAAAA").CGColor;
        _searchBar.layer.borderWidth = 0.5f;
        _searchBar.barTintColor =  HEX_RGB(kBgWhiteColor);
        _searchBar.backgroundColor = HEX_RGB(kBgWhiteColor);
        _searchBar.barStyle = UIBarStyleDefault;
        _searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
        _searchBar.delegate = self;
//        _searchBar.tintColor = [UIColor colorWithHexString:BTShikeMainColor alpha:1.0f];
        UITextField *textField = [_searchBar valueForKey:@"searchField"];
        textField.font = [UIFont systemFontOfSize:13.f];
        textField.backgroundColor =  HEX_RGB(kBgWhiteColor);;
    }
    return _searchBar;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([self.delegate respondsToSelector:@selector(searchBarViewDidTextDidChange: height:)]) {
        [self.delegate searchBarViewDidTextDidChange:searchText height:self.height];
    }
}


@end
